package com.da.sumup.view;

import com.da.sumup.service.IArticleService;
import com.da.sumup.service.ICatalogueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/view")
public class HomeView {

    private final ICatalogueService catalogueService;

    private final IArticleService articleService;

    public HomeView(ICatalogueService catalogueService, IArticleService articleService) {
        this.catalogueService = catalogueService;
        this.articleService = articleService;
    }

    /**
     * home主页面
     */
    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("index");
        mv.addObject("treeList", catalogueService.findAll());
        return mv;
    }

    /**
     * 首页
     */
    @RequestMapping("/main")
    public ModelAndView main(@RequestParam("title") String title) {
        ModelAndView mv = new ModelAndView("main");
        mv.addObject("arts", articleService.findMain(title));
        return mv;
    }

    /**
     * 目录
     */
    @RequestMapping("/dict")
    public ModelAndView dict() {
        ModelAndView mv = new ModelAndView("dict");
        mv.addObject("data", catalogueService.findDict());
        return mv;
    }

    /**
     * home主页面
     */
    @RequestMapping("/article")
    public ModelAndView article(@RequestParam("type") String type) {
        ModelAndView mv = new ModelAndView("article");
        mv.addObject("type", type);
        return mv;
    }

    @RequestMapping("/add")
    public ModelAndView add() {
        ModelAndView mv = new ModelAndView("add");
        mv.addObject("treeList", catalogueService.findAll());
        return mv;
    }

    @RequestMapping("/edit")
    public ModelAndView edit(@RequestParam("uuid")String uuid) {
        ModelAndView mv = new ModelAndView("edit");
        mv.addObject("article",articleService.findOne(uuid));
        mv.addObject("treeList", catalogueService.findAll());
        return mv;
    }

    @RequestMapping("/detail")
    public ModelAndView detail(@RequestParam("uuid") String uuid) {
        ModelAndView mv = new ModelAndView("detail");
        mv.addObject("article",articleService.findOne(uuid));
        return mv;
    }

}
